package com.flycms.module.cms.service;

import com.flycms.core.entity.PageVo;
import com.flycms.module.cms.dao.CustomDao;
import com.flycms.module.cms.model.Custom;
import com.flycms.module.cms.param.CustomParam;
import com.flycms.module.cms.param.NewOrder;
import com.flycms.utils.ConvertUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huangzt
 * @version 1.0
 * @date 2020/3/2 17:07
 */
@Service
public class CustomService {
    @Autowired
    CustomDao customDao;

    public int insert(Custom custom){
        return customDao.insertSelective(custom);
    }


    public Custom queryByPK(long id){
        return customDao.queryByPk(id);
    }

    public PageVo<Custom> queryByPage(CustomParam param){
        //統計
        int count = customDao.count(param);
        if(count <= 0){
            return new PageVo<>(param.getPageIndex());
        }
        List<Custom> customs =
                customDao.queryByPage(param, param.getPageIndex() * param.getPageSize(), param.getPageSize());
        return new PageVo<>(param.getPageIndex(),count,customs);
    }

    /**
     * 新订单保存
     * @param order 订单信息
     */
    public void insert(NewOrder order) {
        Custom custom = ConvertUtils.sourceToTarget(order, Custom.class);
        customDao.insertSelective(custom);
        order.setCustomId(custom.getId());
    }

    /**
     * 根据ID集合查询数据
     * @param ids
     * @return
     */
    public List<Custom> selectByIds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Lists.newArrayList();
        }
        return customDao.selectByIds(ids);
    }
}
